package com.gitee.cirnochat.user.controller;

import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.PageVo;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.user.model.vo.resp.UserVo;
import com.gitee.cirnochat.user.service.UserBlockListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户拉黑接口", description = "用户拉黑相关API")
@RestController
@RequestMapping("/user/block")
@RequiredArgsConstructor
public class UserBlockController {
    private final UserBlockListService userBlockListService;

    @Operation(summary = "获取被拉黑用户", description = "获取被拉黑用户")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = UserVo.class)
            )
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @GetMapping
    public Result<PageVo<UserVo>> listBlockUsers(@Parameter(description = "页码",  example = "1") @RequestParam(defaultValue = "1", required = false) int page,
                                                 @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "10" , required = false) int size,
                                                 @Parameter(description = "排序方式 1:升序 2:降序", example = "2")  @RequestParam(defaultValue = "2",required = false) int sort,
                                                 HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(userBlockListService.pageBlockUsers(page,size,sort,userId));
    }

    @Operation(summary = "拉黑用户", description = "拉黑用户")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @PostMapping("/{userId}")
    public Result<Void> addBlockUser(@PathVariable Long userId , HttpServletRequest request) {
        Long currentUserId = RequestUtils.getUserId(request);
        userBlockListService.addBlockUser(userId,currentUserId);
        return Result.success();
    }

    @Operation(summary = "取消拉黑用户", description = "取消拉黑用户")
    @ApiResponse(
            responseCode = "200",
            description = "成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Result.class)
            )
    )
    @AuthCheck
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @DeleteMapping("/{userId}")
    public Result<Void> removeBlockUser(@PathVariable Long userId , HttpServletRequest request) {
        Long currentUserId = RequestUtils.getUserId(request);
        userBlockListService.removeBlockUser(userId,currentUserId);
        return Result.success();
    }
}
